﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Ewelve_Mouth_HD.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GetFileController : ControllerBase
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpFile(IFormFile file)
        {
            try
            {
                var T = file.ContentType;
                var R = new List<string>
                {
                    "image/img",
                    "image/png",
                    "image/img",
                    "image/jpg",
                    "image/jpeg",
                };
                if (!R.Contains(T))
                {
                    return Ok("您上传的文件不符合类型，请上传图片！");
                }
                var Y = 1024 * 1024 * 2;
                if(file.Length > Y)
                {
                    return Ok("您上传的图片超过2M");
                }
                var Str=Guid.NewGuid().ToString();
                var G = Directory.GetCurrentDirectory() + @"/wwwroot/imgs/" + Str + file.FileName;
                using (FileStream GH = new FileStream(G, FileMode.Create))
                {
                    file.CopyTo(GH);
                    GH.Flush();
                }
                return Ok("https://localhost:7061" + "/imgs/"+ Str + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
